﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using AutoLoL.Logic.Parsers;
using AutoLoL.Logic.Properties;
using AutoLoL.Logic.Utils;

namespace AutoLoL.Logic.Masteries.Importers
{
    public class LeagueCraftMasteryImporter : IMasteryImporter
    {
        public string Name
        {
            get { return "LeagueCraft.com"; }
        }

        public BitmapSource Icon
        {
            get { return ImageParser.BitmapToBitmapSource(Resources.leaguecraft); }
        }

        public bool Select(string input)
        {
            return input.ToLower().Contains("leaguecraft.com");
        }

        public string Import(string source)
        {
            try
            {
                source = source.ToLower();

                string prefix = "leaguecraft.com/masteries/";

                if (source.Contains(prefix))
                {
                    // Chopping off the prefix from the url should be enough
                    source = source.Substring(source.IndexOf(prefix) + prefix.Length);
                    return source;
                }
                else if (source.Contains("leaguecraft.com/strategies/guide/"))
                {
                    // Get the html text
                    string text = Web.GetWebText(source);

                    while (true)
                    {
                        // Find a url to the leaguecraft mastery builder
                        int startIndex = text.IndexOf(prefix);
                        if (startIndex >= 0)
                        {
                            // Chop off all preceeding text
                            text = text.Substring(startIndex + prefix.Length);

                            int temp = 0;

                            // If the first character is a digit we'll use this input
                            if (int.TryParse(text.Substring(0, 1), out temp))
                            {
                                int index = 1;

                                // Find and return all digits
                                while (true)
                                {
                                    if (index < text.Length && int.TryParse(text.Substring(index, 1), out temp))
                                    {
                                        index++;
                                    }
                                    else
                                    {
                                        return text.Substring(0, index);
                                    }
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            catch { if (Debugger.IsAttached) throw; }

            return null;
        }
    }
}